package com.cy.personer.web;

import com.cy.personer.bean.FirstMenu;
import com.cy.personer.bean.SecondMenu;
import com.cy.personer.bean.Work;
import com.cy.personer.biz.impl.MenuBizIpml;
import com.cy.personer.biz.impl.WorkBizImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Controller
@RequestMapping("/login")
public class LoginHandler {
    @Resource
    private WorkBizImpl workBizImpl;
    @Resource
    private MenuBizIpml menuBizIpml;
    @Resource
    private MD5Encoder md5Encoder;

    @RequestMapping(value="/checkLogin.action", method=RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody String checkLogin(HttpServletRequest request, Work work1) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String result = "login";
        HttpSession session = request.getSession();
//        ServletContext application = session.getServletContext();
        String keycode = work1.getKeyCode().toUpperCase();
        String sessionCode = (String) request.getSession().getAttribute("keyCode");
//        Work work2 = (Work) application.getAttribute("work");
//        if (work2 == null) {

        if(keycode.equals(sessionCode)){
            String password = md5Encoder.EncoderByMd5(work1.getWorkPwd());
            work1.setWorkPwd(password);
            Work work = workBizImpl.login(work1);
            if(work != null){
                if(work.getStateId()==0||work.getStateId()==2){
                    result = "stateError";//帐号状态不可用
                }else{
                    session.setAttribute("work",work);
//                    application.setAttribute("work",work);
                    List<FirstMenu> firstMenus = menuBizIpml.queryFirstMenu(work.getRoleId());
                    session.setAttribute("firstMenus",firstMenus);
                    List<SecondMenu> secondMenus = menuBizIpml.querySecondMenu(work.getRoleId());
                    session.setAttribute("secondMenus",secondMenus);
                    result = "success";
                }
            }else{
                result = "loginError";//帐号或密码错误
            }
        }else{
            result = "codeError";//验证码错误
        }
//        }else {
//            result = "existence";//帐号已经登录
//        }
        return result;
    }

    //登录
    @RequestMapping(value="/login.action")
    public String loginUser(){
        return "admin/main";
    }

    //退出
    @RequestMapping(value="/out.action")
    public void out(HttpServletRequest request,HttpServletResponse resp) throws IOException {
        HttpSession session = request.getSession();
        ServletContext application = session.getServletContext();
        session.removeAttribute("work");
        application.removeAttribute("work");
        resp.sendRedirect(request.getContextPath()+"/admin_login.jsp");
//        return "admin/admin_login";
    }

    public WorkBizImpl getWorkBizImpl() {
        return workBizImpl;
    }

    public void setWorkBizImpl(WorkBizImpl workBizImpl) {
        this.workBizImpl = workBizImpl;
    }

    public MenuBizIpml getMenuBizIpml() {
        return menuBizIpml;
    }

    public void setMenuBizIpml(MenuBizIpml menuBizIpml) {
        this.menuBizIpml = menuBizIpml;
    }
}
